/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
import dayjs from 'dayjs';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD';

export function formatToDateTime(date, format = DATE_TIME_FORMAT): string {
  return dayjs(date).format(format);
}

export function formatToDate(date, format = DATE_FORMAT): string {
  return dayjs(date).format(format);
}

export function getYears(): string[] {
  const years = [];
  let index = 10;
  const currentYear = formatToDate(new Date(), 'YYYY');
  years.push(parseInt(currentYear));
  while (index > 0) {
    years.push(parseInt(currentYear) + index);
    years.push(parseInt(currentYear) - index);
    index--;
  }
  return years.sort();
}

export function getYearsSelect(): Object[] {
  const years = getYears();
  const yearsSelect = [];
  years.forEach((item) => {
    yearsSelect.push({
      label: item,
      value: item,
    });
  });
  return yearsSelect;
}

export const dateUtil = dayjs;
